#此类是自定义的权限类
"""
1,继承BasePermission
2、实现has_permission方法，根据自定义规则实现权限，全局或局部配置
"""

from rest_framework.permissions import  BasePermission
from api.models import Employee

class MyPermission(BasePermission):
    """
    有权访问返回True
    无权访问返回False
    登录可以写,游客只能读
    """

    def has_permission(self, request, view):
        #只读接口,所有人可访问
        if request.method in ('GET',"HEAD","OPTIONS"):
            return True

        name=request.data.get("name")
        user=Employee.objects.filter(name=name).first()

        if user:
            return True
        return False
